/**
 * @class   wosh::gui::WidgetNEW_List
 * @brief   Representation of a NEW_List as a QListWidget and a custom
 *          ListModel.
 *          
 *
 ****************************************************************************
 * @version $Id: WidgetDataList.h 2189 2010-03-22 21:22:06Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetNEW_List_H__
 #define __WOSH_Gui_WidgetNEW_List_H__

 #include <core/NEW_List.h>
 #include <framework/gui/GuiUtilities.h>

 #include <QAbstractListModel>
 #include <QtGui/QListWidget>


namespace wosh {
 namespace gui {


class ListModel :  public QAbstractListModel  {

	Q_OBJECT

	public:

		ListModel(const wosh::NEW_List* list, QObject *parent = 0) : QAbstractListModel(parent)
		 {
			this->dataList = NULL;
			loadTable( list );
		 }

		virtual ~ListModel()
		 {
			if ( this->dataList != NULL )
				delete this->dataList; this->dataList = NULL;
		 }

	public:

		void loadTable( const wosh::NEW_List* list )
		 {
			if ( this->dataList != NULL )
				delete this->dataList; this->dataList = NULL;
			if ( list == NULL )
				return;
			this->dataList = list->clone();
		 }

		void acquireTable( wosh::NEW_List* list )
		 {
			if ( this->dataList != NULL )
				delete this->dataList; this->dataList = NULL;
			this->dataList = list;
		 }


	public:

		int rowCount(const QModelIndex &parent = QModelIndex()) const
		 {
			(void)parent;
			if ( this->dataList == NULL ) return 0;
			return this->dataList->size();
		 }

		QVariant data(const QModelIndex &index, int role) const
		 {
			if ( this->dataList == NULL || !index.isValid() )
				return QVariant();

			if ( role == Qt::DisplayRole ) {
				return GuiUtilities::convertSerializable2QVariant(this->dataList->itemAt(index.row()));
			 }
			else if ( role == Qt::ToolTipRole ) {
				return GuiUtilities::convertSerializable2QVariant(this->dataList->itemAt(index.row()));
			 }
			return QVariant();
		 }


	public: // editable 

		 Qt::ItemFlags flags(const QModelIndex &index) const
		  {
			 if (!index.isValid())
				 return Qt::ItemIsEnabled;
			 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
		  }

		bool setData(const QModelIndex &index, const QVariant &value, int role)
		{
			if ( this->dataList == NULL ) return false;
			if ( !index.isValid() || role != Qt::EditRole)
				return false;

//			value = GuiUtilities::convertSerializable2QVariant( this->dataList->indexOf(index.row()) );
			emit dataChanged(index, index);
			return true;
		}


	protected:
		wosh::NEW_List* dataList;


}; // class def



class WidgetNEW_List : public QListWidget  {

	Q_OBJECT

	public:

		WidgetNEW_List(QWidget *parent = 0) : QListWidget(parent) { }
		virtual ~WidgetNEW_List() { }

	public:

		virtual WRESULT loadList( const NEW_List* datalist )
		 {
			this->clear();
			if ( datalist == NULL ) return WRET_OK;

			const Variant* data;
			for(int i=0; i<datalist->size(); i++){
				data = dynamic_cast<const Variant*>(datalist->itemAt(i));
				if ( data == NULL ) continue;
				this->addItem( QString(data->toString().c_str()) );
			 }

			return WRET_OK;
		 }


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetNEW_List_H__
